clear 

* set directory 
* cd "directory"

* set paths
global datafolder "data"
global outputfolder "output"

********************************************************************************
*			Figure 5: Pass-through coefficient time-series
********************************************************************************
	use "$datafolder\maindata.dta", clear
	
	keep if datem >= 660
	
	gen date_d = dofw(datew)
	format date_d %td
		
	* Coefficient plot (monthly)
	gen passthrough_m = .
	gen passthrough_m_sd = .
	gen obs = .
	
	forvalues dt = 660/671{
		reg actual_rate c.theoretical_rate if datem==`dt', cluster(customerid)
					
		replace passthrough_m = _b[theoretical_rate] if e(sample)
		replace passthrough_m_sd = _se[theoretical_rate] if e(sample)
		replace obs = e(N) if e(sample)
	
	}
	
	
	preserve
	
	
	collapse (mean) passthrough_m passthrough_m_sd obs (min) date_d, ///
		by(datem)
	
	gen passthrough_ub = passthrough_m + 1.96 * passthrough_m_sd
	gen passthrough_lb = passthrough_m - 1.96 * passthrough_m_sd
	
	twoway (connected passthrough_m date_d if inrange(datem, 660,671), ///
		color(gray) ylabel(#6) yscale(range(-1(0.25)1.5))) ///
		(rcap passthrough_ub passthrough_lb  date_d if inrange(datem, 660,671),  ///
		graphregion(color(white)) color(gray) mfcolor(none) ///
		xtitle(" ") ytitle(" ") title(" ") ///
		ylabel(,angle(horizontal)) yline(0, lp(dash)) ///
		tline(01may2015, lp(dash) lc(black)) /// 
		tline(26june2015, lp(dash) lc(black)) /// 
		tline(13july2015, lp(dash) lc(black)) legend(off)) 
		
		graph export "$outputfolder\figure_5_R.pdf", ///
		as(pdf) replace
		graph export "$outputfolder\figure_5_R.png", ///
		as(png) replace
	
	
	restore
	
	
	
	* Coefficient plot (weekly)
	gen passthrough_wk = .
	gen passthrough_wk_sd = .
	capture drop obs
	gen obs = .
	
	forvalues dt = 2866/2885{
		reg actual_rate c.theoretical_rate if datew==`dt', cluster(customerid)
		
		replace passthrough_wk = _b[theoretical_rate] if e(sample)
		replace passthrough_wk_sd = _se[theoretical_rate] if e(sample)
		replace obs = e(N) if e(sample)
	
	}
	
	forvalues dt = 2888/2907{
		reg actual_rate c.theoretical_rate if datew==`dt', cluster(customerid)
		
		replace passthrough_wk = _b[theoretical_rate] if e(sample)
		replace passthrough_wk_sd = _se[theoretical_rate] if e(sample)
		replace obs = e(N) if e(sample)
	
	}
	
	
	collapse (mean) passthrough_wk passthrough_wk_sd obs (min) date_d, ///
		by(datew)
	
	gen passthrough_ub = passthrough_wk + 1.96 * passthrough_wk_sd
	gen passthrough_lb = passthrough_wk - 1.96 * passthrough_wk_sd
	
	twoway (connected passthrough_wk date_d if inrange(datew, 2866, 2907), ///
		color(gray) ylabel(#6) yscale(range(-1(0.25)1.5)) ) ///
		(rcap passthrough_ub passthrough_lb  date_d if inrange(datew, 2866, 2907),  ///
		graphregion(color(white)) color(gray) mfcolor(none) ///
		xtitle(" ") ytitle(" ") title(" ") ///
		ylabel(,angle(horizontal)) yline(0, lp(dash)) ///
		tline(01may2015, lp(dash) lc(black)) /// 
		tline(26june2015, lp(dash) lc(black)) /// 
		tline(13july2015, lp(dash) lc(black)) legend(off)) 
		
		graph export "$outputfolder\figure_5_L.pdf", ///
		as(pdf) replace
		graph export "$outputfolder\figure_5_L.png", ///
		as(png) replace
	
	
	